﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using MsTestUtilities.Utilities;
using System;

namespace MsTestUtilities.RowTests
{
    internal class StandardAssertExpectation<E> : IExpectation<E>
    {
        private readonly Action<E> _assertion;

        public StandardAssertExpectation(Action<E> assertion)
        {
            _assertion = assertion;
        }

        public StandardAssertExpectation(E expected, Action<E, E> asserter)
        {
            _assertion = actual => asserter(expected, actual);
        }

        public StandardAssertExpectation(Action<E, E> asserter, E expected)
        {
            _assertion = actual => asserter(actual, expected);
        }

        public AssertionResult IsSatisfiedFor(Func<E> actualGenerator)
        {
            try
            {
                _assertion(actualGenerator());
                return AssertionResult.Success;
            }
            catch (AssertFailedException)
            {
                throw;
            }
            catch (Exception ex)
            {
                return AssertionResult.Error("An unexpected exception was trown: {0}", ex);
            }
        }
    }
}
